<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="120px">
      <el-form-item label="环节名称" prop="detailsMc">
        <el-input v-model="dataForm.detailsMc" placeholder="环节名称"></el-input>
      </el-form-item>
      <el-form-item label="操作权限" prop="businessPemission">
        <!-- <el-input v-model="dataForm.businessPemission" placeholder="环节名称"></el-input> -->
        <el-select style="width: 100%;" :clearable="true" v-model="dataForm.businessPemission" placeholder="请选择操作权限">
          <el-option v-for="item in businessPemissions " :key="item.value" :label="item.label" :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审批人员名称" prop="examineUserName">
        <el-input :disabled="true" v-model="dataForm.examineUserName" placeholder="审批人员名称">
          <el-button slot="append" @click="open" icon="el-icon-search">请选择审批人</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="环节说明" prop="detailsSm">
        <el-input type="textarea" v-model="dataForm.detailsSm" placeholder="环节说明"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
    <checkUser ref="checkUser" :multiple='false' @getUserList="getUserList"></checkUser>
  </el-dialog>
</template>
<script>
  import {
    getObj,
    addObj,
    putObj
  } from '@/api/admin/sysexaminedetails'
  import {
    remote as remoteDict
  } from '@/api/admin/dict'
  import checkUser from '@/components/form/compound/selectUser'

  export default {
    components: {
      checkUser
    },
    data() {
      return {
        businessPemissions: [{label:'可编辑',value:'可编辑'},{label:'无',value:'无'}],
        visible: false,
        canSubmit: false,
        dataForm: {
          detailsId: 0,
          examineId: '',
          detailsMc: '',
          detailsSm: '',
          examineUserName: '',
          examineUserId: '',
          businessPemission:''
        },
        dataRule: {
          detailsMc: [{
            required: true,
            message: '环节名称不能为空',
            trigger: 'blur'
          }],
          // detailsSm: [{
          //   required: true,
          //   message: '环节说明不能为空',
          //   trigger: 'blur'
          // }],
          examineUserName: [{
            required: true,
            message: '审批人员名称不能为空',
            trigger: 'blur'
          }],
           businessPemission: [{
            required: true,
            message: '操作权限不能为空',
            trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      init(examineId, id, detailsPid) {
        this.dataForm.detailsId = id || 0;
        this.dataForm.examineId = examineId;
        this.dataForm.detailsPid = detailsPid;
        this.visible = true;
        this.canSubmit = true;
       //console.log(1)
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.detailsId) {
            getObj(this.dataForm.detailsId).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      open() {
        this.$refs.checkUser.init();
      },
      getUserList(data) {
        var obj = data[0];
        this.dataForm.examineUserId = obj.userId;
        this.dataForm.examineUserName = obj.ryName;
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.detailsId) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      }
    }
  }

</script>
